{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.067508500Z",
     "start_time": "2024-09-29T12:17:00.977145400Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------- 一维数组 ----------\n",
      "[1 2 3 4 5]\n",
      "<class 'numpy.ndarray'>\n",
      "---------- 二维数组 ----------\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "<class 'numpy.ndarray'>\n",
      "数组的形状：(2, 3)\n",
      "数组的维度：2\n",
      "数组的数据类型：int64\n",
      "---------- 指定类型 ----------\n",
      "[1.1 2.2 3.3 4.4 5.5]\n",
      "数组的数据类型：float32\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "1. 数组的创建\n",
    "\"\"\"\n",
    "\n",
    "# 一维数组\n",
    "print('---------- 一维数组 ----------')\n",
    "arr1 = np.array([1, 2, 3, 4, 5])\n",
    "print(arr1)\n",
    "print(type(arr1))\n",
    "\n",
    "# 二维数组\n",
    "print('---------- 二维数组 ----------')\n",
    "arr2 = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(arr2)\n",
    "print(type(arr2))\n",
    "print(f'数组的形状：{arr2.shape}')  # 2行3列，打印 (2,3)\n",
    "print(f'数组的维度：{arr2.ndim}')  # 2维数组，打印 2\n",
    "print(f'数组的数据类型：{arr2.dtype}')  # 数组类型\n",
    "\n",
    "# 指定类型\n",
    "print('---------- 指定类型 ----------')\n",
    "arr3 = np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float32)\n",
    "print(arr3)\n",
    "print(f'数组的数据类型：{arr3.dtype}')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.094508Z",
     "start_time": "2024-09-29T12:17:01.072508100Z"
    }
   },
   "id": "18dedfb241dcc613"
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------- 一维数组切片 ----------\n",
      "[4 5 6]\n",
      "[1 3 5]\n",
      "---------- 二维数组切片 ----------\n",
      "[[1 2 3]\n",
      " [3 4 5]\n",
      " [5 6 7]]\n",
      "[1 3 5]\n",
      "[[3 4]\n",
      " [5 6]\n",
      " [7 8]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "2. 数组索引和切片\n",
    "\"\"\"\n",
    "\n",
    "# 一维数组切片\n",
    "print('---------- 一维数组切片 ----------')\n",
    "arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\n",
    "print(arr1[3:6])\n",
    "print(arr1[:6:2])\n",
    "\n",
    "# 二维数组切片\n",
    "print('---------- 二维数组切片 ----------')\n",
    "arr2 = np.array([[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10, 11]])\n",
    "print(arr2[0:3])  # 0-2行\n",
    "print(arr2[:3, 0])  # 0-2行，0列\n",
    "print(arr2[1:4, 0:2])  # 1-3行，0-1列"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.129664400Z",
     "start_time": "2024-09-29T12:17:01.088508400Z"
    }
   },
   "id": "6977d6e3d4befce9"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------- 基本运算 ----------\n",
      "[1, 2, 3, 4, 5, 6]\n",
      "[5 7 9]\n",
      "[-1  0  1]\n",
      "[2 4 6]\n",
      "[2. 3. 3.]\n",
      "---------- 线性代数 ----------\n",
      "32\n",
      "3.7416573867739413\n",
      "[-3  6 -3]\n",
      "---------- 统计 ----------\n",
      "数组的平均值：2.0\n",
      "数组的平均值：2.0\n",
      "数组的和：6\n",
      "数组的最大值：3\n",
      "数组的最小值：1\n",
      "数组的标准差：0.816496580927726\n",
      "数组的排序：[[ 2  4  5]\n",
      " [ 2  3  9]\n",
      " [ 1  8 10]\n",
      " [ 2  9 13]]\n",
      "---------- 条件筛选 ----------\n",
      "[False False False False False  True  True  True  True  True]\n",
      "[ 6  7  8  9 10]\n",
      "[3 4]\n",
      "[ 1  8  9 10]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "3. 数组运算\n",
    "\"\"\"\n",
    "\n",
    "# 基本运算\n",
    "print('---------- 基本运算 ----------')\n",
    "print([1, 2, 3] + [4, 5, 6])  # 列表运算结果：[1, 2, 3, 4, 5, 6]\n",
    "print(np.array([1, 2, 3]) + np.array([4, 5, 6]))  # np数组运算结果：[5 7 9]\n",
    "print(np.array([1, 2, 3]) - 2)\n",
    "print(np.array([1, 2, 3]) * 2)\n",
    "print(np.array([10, 9, 6]) / np.array([5, 3, 2]))\n",
    "\n",
    "# 线性代数\n",
    "print('---------- 线性代数 ----------')\n",
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([4, 5, 6])\n",
    "\n",
    "print(np.dot(arr1, arr2))  # 点乘\n",
    "print(np.linalg.norm(arr1))  # 向量模\n",
    "print(np.cross(arr1, arr2))  # 叉乘\n",
    "\n",
    "# 统计\n",
    "print('---------- 统计 ----------')\n",
    "print(f'数组的平均值：{arr1.mean()}')  # 平均值写法1\n",
    "print(f'数组的平均值：{np.mean(arr1)}')  # 平均值写法2\n",
    "print(f'数组的和：{arr1.sum()}')  # 同样两种写法，之后不再赘述\n",
    "print(f'数组的最大值：{arr1.max()}')\n",
    "print(f'数组的最小值：{arr1.min()}')\n",
    "print(f'数组的标准差：{arr1.std()}')\n",
    "arr3 = np.array([[4, 2, 5], [3, 2, 9], [1, 8, 10], [13, 2, 9]])\n",
    "print(f'数组的排序：{np.sort(arr3)}')  # 按行排序 只有一种写法\n",
    "\n",
    "# 条件筛选\n",
    "print('---------- 条件筛选 ----------')\n",
    "arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\n",
    "print(arr1 > 5)\n",
    "print(arr1[arr1 > 5])\n",
    "print(arr1[(arr1 > 2) & (arr1 < 5)])  # and操作要用按位与\n",
    "print(arr1[(arr1 < 2) | (arr1 > 7)])  # or操作要用按位或"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.130663800Z",
     "start_time": "2024-09-29T12:17:01.103508Z"
    }
   },
   "id": "a5abadd4440ab54b"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "原形状：(4, 3)\n",
      "新数组：[[ 1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12]]\n",
      "新形状：(2, 6)\n",
      "转置后：[[ 1  7]\n",
      " [ 2  8]\n",
      " [ 3  9]\n",
      " [ 4 10]\n",
      " [ 5 11]\n",
      " [ 6 12]]\n",
      "一维数组：[ 1  7  2  8  3  9  4 10  5 11  6 12]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "4. 数组形状\n",
    "\"\"\"\n",
    "\n",
    "arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])\n",
    "print(f'原数组：{arr2}')\n",
    "print(f'原形状：{arr2.shape}')\n",
    "arr2 = arr2.reshape(2, 6)\n",
    "print(f'新数组：{arr2}')\n",
    "print(f'新形状：{arr2.shape}')\n",
    "arr2 = arr2.transpose()\n",
    "print(f'转置后：{arr2}')\n",
    "arr2 = arr2.reshape(-1) # -1表示转换成一维数组\n",
    "print(f'一维数组：{arr2}')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.133663300Z",
     "start_time": "2024-09-29T12:17:01.118507900Z"
    }
   },
   "id": "ff6eb68d13d64f67"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "5. 数据保存与导入\n",
    "\"\"\"\n",
    "\n",
    "arr1 = np.array([1, 2, 3, 4, 5])\n",
    "np.save('arr.npy', arr1)\n",
    "\n",
    "arr2 = np.load('arr.npy')\n",
    "print(arr2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.190185300Z",
     "start_time": "2024-09-29T12:17:01.132664100Z"
    }
   },
   "id": "81d4d320171f35b0"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------- 随机生成数组 ----------\n",
      "随机数：0.417022004702574\n",
      "[7.20324493e-01 1.14374817e-04 3.02332573e-01 1.46755891e-01\n",
      " 9.23385948e-02 1.86260211e-01 3.45560727e-01 3.96767474e-01\n",
      " 5.38816734e-01 4.19194514e-01]\n",
      "[[0.6852195  0.20445225 0.87811744 0.02738759 0.67046751]\n",
      " [0.4173048  0.55868983 0.14038694 0.19810149 0.80074457]]\n",
      "[0.96826158 0.31342418 0.69232262 0.87638915 0.89460666 0.08504421\n",
      " 0.03905478 0.16983042 0.8781425  0.09834683]\n",
      "[[0.42110763 0.95788953 0.53316528 0.69187711 0.31551563]\n",
      " [0.68650093 0.83462567 0.01828828 0.75014431 0.98886109]]\n",
      "---------- 生成正态分布的随机数或随机数数组 ----------\n",
      "[-0.85005238  0.96082    -0.21741818  0.15851488  0.87341823 -0.11138337\n",
      " -1.03803876 -1.00947983 -1.05825656  0.65628408]\n",
      "[[-0.06249159 -1.73865429  0.103163   -0.62166685  0.27571804]\n",
      " [-1.09067489 -0.60998525  0.30641238  1.69182613 -0.74795374]]\n",
      "---------- 生成上下范围限定的随机数 ----------\n",
      "[4 1 9 7 5 6 7 3 6 8]\n",
      "[[63 58 61 62 93]\n",
      " [70 80 86 89 57]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "6. 随机数生成\n",
    "\"\"\"\n",
    "\n",
    "# 设置随机种子\n",
    "np.random.seed(1)\n",
    "# 随机生成数组\n",
    "print('---------- 随机生成数组 ----------')\n",
    "print(f'随机数：{np.random.random()}')\n",
    "arr1 = np.random.random(10) # 生成10个随机数数组，随机数范围[0,1)\n",
    "print(arr1)\n",
    "arr2 = np.random.random((2, 5)) # 生成2行5列的二维随机数数组，随机数范围[0,1)\n",
    "print(arr2)\n",
    "arr1 = np.random.rand(10) # 作用和上面一样\n",
    "print(arr1)\n",
    "arr2 = np.random.rand(2, 5)\n",
    "print(arr2)\n",
    "\n",
    "# 生成正态分布（高斯分布）的随机数或随机数数组\n",
    "print('---------- 生成正态分布的随机数或随机数数组 ----------')\n",
    "arr1 = np.random.randn(10)\n",
    "print(arr1)\n",
    "arr2 = np.random.randn(2, 5)\n",
    "print(arr2)\n",
    "\n",
    "# 生成上下范围限定的随机数\n",
    "print('---------- 生成上下范围限定的随机数 ----------')\n",
    "arr1 = np.random.randint(low=1, high=10, size=10, dtype=np.int32)\n",
    "print(arr1)\n",
    "arr2 = np.random.randint(50, 100, (2, 5))\n",
    "print(arr2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.192289Z",
     "start_time": "2024-09-29T12:17:01.153185900Z"
    }
   },
   "id": "b39364323b1eeeee"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初始数组：[[ 6 12 13  9]\n",
      " [10 12  6 16]\n",
      " [ 1 17  2 13]\n",
      " [ 8 14  7 19]]\n",
      "筛选10以内的数后：[6 9 6 1 2 8 7]\n",
      "所有数的和：39\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "案例：\n",
    "    生成一个4×4的随机数组，只保留10以内的数，并计算出所有元素的和\n",
    "\"\"\"\n",
    "\n",
    "import numpy as np\n",
    "np.random.seed(1)\n",
    "arr = np.random.randint(1, 20, (4, 4))\n",
    "print(f'初始数组：{arr}')\n",
    "arr = arr[arr<10]\n",
    "print(f'筛选10以内的数后：{arr}')\n",
    "print(f'所有数的和：{np.sum(arr)}')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.204530300Z",
     "start_time": "2024-09-29T12:17:01.178185600Z"
    }
   },
   "id": "725c0e51933d94f4"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-29T12:17:01.259642900Z",
     "start_time": "2024-09-29T12:17:01.196289700Z"
    }
   },
   "id": "59a721636a3764c0"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
